#include "progressTip.h"
#include <QPainter>
#include <QColor>

ProgressTip::ProgressTip(QWidget *parent) :
    QWidget(parent)
{
    //初始化变量值
    minValue = 0.0;
    maxValue = 100.0;
    value = 0.0;

    percent = true;
    padding = 20;
    radius  = 10;

    valueBrush = QBrush(Qt::NoBrush);
    valueColor = QColor("#22a3a9");
    bgColor = QColor("#b3b3b3");
    textColor = QColor("#ffffff");
    tipColor = QColor("#22a3a9");
}

void ProgressTip::paintEvent(QPaintEvent *)
{
    //启用反锯齿，平移坐标轴中心，等比例缩放
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //绘制背景
    drawBg(&painter);
    //绘制是部分提示信息
    drawTip(&painter);
    //绘制进度
    drawValue(&painter);
}

void ProgressTip::drawBg(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(bgColor);

    QRect rect(padding, (height() / 3) * 2, width() - padding*2,height()/3);
    painter->drawRoundedRect(rect,radius,radius);

    painter->restore();
}

void ProgressTip::drawTip(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(valueBrush == Qt::NoBrush ? tipColor : valueBrush);

    //计算当前值的百分比
    double step = value / (maxValue - minValue);
    int progress = (width() - padding * 2) * step;

    //绘制上部分提示信息背景
    QRect rect(progress,0,padding * 2,(double)height() / 2.1);
    painter->drawRoundedRect(rect,2,2);

    //绘制倒三角
    int centerX = rect.center().x();
    int initY = rect.height();
    int offset = 5;
    QPolygon pts;
    pts.append(QPoint(centerX - offset,initY));
    pts.append(QPoint(centerX + offset,initY));
    pts.append(QPoint(centerX,initY + offset));
    painter->drawPolygon(pts);

    //绘制文字
    QString strValue;
    if(percent){
        double temp = value / (maxValue - minValue) * 100;
        strValue = QString("%1%").arg(temp,0,'f',0);
    }else{
        strValue = QString("%1").arg((double)value,0,'f',0);
    }
    painter->setPen(textColor);
    painter->drawText(rect,Qt::AlignCenter,strValue);

    painter->restore();
}

void ProgressTip::drawValue(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);

    //定义了画刷去画刷，可以形成渐变效果
    painter->setBrush(valueBrush == Qt::NoBrush ? valueColor : valueBrush);

    //计算当前值的百分比
    double step = value / (maxValue - minValue);
    int progress = (width() - 2*padding) * step;

    QRect rect(padding,(height() / 3)*2,progress,height() / 3);
    painter->drawRoundedRect(rect,radius,radius);

    painter->restore();
}

double ProgressTip::getMinValue() const
{
    return minValue;
}

double ProgressTip::getMaxValue() const
{
    return maxValue;
}

double ProgressTip::getValue() const
{
    return value;
}

bool ProgressTip::getPercent() const
{
    return percent;
}

int ProgressTip::getPadding() const
{
    return padding;
}

int ProgressTip::getRadius() const
{
    return radius;
}

QBrush ProgressTip::getValueBrush() const
{
    return valueBrush;
}

QColor ProgressTip::getValueColor() const
{
    return valueColor;
}

QColor ProgressTip::getBgColor() const
{
    return bgColor;
}

QColor ProgressTip::getTextColor() const
{
    return textColor;
}

QColor ProgressTip::getTipColor() const
{
    return tipColor;
}

QSize ProgressTip::sizeHint() const
{
    return QSize(300,50);
}

QSize ProgressTip::minimumSizeHint() const
{
    return QSize(50,30);
}

void ProgressTip::setRange(double minValue, double maxValue)
{
    if(minValue >= maxValue)
    {
        return;
    }
    this->maxValue = maxValue;
    this->minValue = minValue;

    if(value < minValue){
        setValue(minValue);
    }else if(value > maxValue){
        setValue(maxValue);
    }

    this->update();
}

void ProgressTip::setRange(int minValue, int maxValue)
{
    setRange((double)minValue,(double)maxValue);
}

void ProgressTip::setMinValue(const double &minValue)
{
    setRange((double)minValue,(double)maxValue);
}

void ProgressTip::setMaxValue(const double &maxValue)
{
    setRange((double)minValue,(double)maxValue);
}

void ProgressTip::setValue(double value)
{
    if(value == this->value){
        return;
    }

    if(value < minValue){
        value = minValue;
    }else if(value > maxValue){
        value = maxValue;
    }
    this->value = value;
    this->update();
    emit valueChanged(value);
}

void ProgressTip::setValue(int value)
{
    setValue((double)value);
}

void ProgressTip::setPercent(const bool &percent)
{
    if(this->percent != percent){
        this->percent = percent;
        this->update();
    }
}

void ProgressTip::setPadding(const int &padding)
{
    if(this->padding != padding){
        this->padding = padding;
        this->update();
    }

}

void ProgressTip::setRadius(const int &radius)
{
    if(this->radius != radius){
        this->radius = radius;
        this->update();
    }
}

void ProgressTip::setValueBrush(const QBrush &valueBrush)
{
    if(this->valueBrush != valueBrush){
        this->valueBrush = valueBrush;
        this->update();
    }
}

void ProgressTip::setValueColor(const QColor &valueColor)
{
    if(this->valueColor != valueColor){
        this->valueColor = valueColor;
        this->update();
    }
}

void ProgressTip::setBgColor(const QColor &bgColor)
{
    if(this->bgColor != bgColor){
        this->bgColor = bgColor;
        this->update();
    }
}

void ProgressTip::setTextColor(const QColor &textColor)
{
    if(this->textColor != textColor){
        this->textColor = textColor;
        this->update();
    }
}

void ProgressTip::setTipColor(const QColor &tipColor)
{
    if(this->tipColor != tipColor){
        this->tipColor = tipColor;
        this->update();
    }
}
